﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace RCREEE.Library.Entities
{
    public class Contact
    {
        #region Private fields

        #region Contact Master

        private int _contactId;

        private int _countryId;
        private string _countryName;

        private int _entityId;
        private string _entityName;

        private int _parentContactId;
        private string _parentContactName;

        private int _parentContactEntityId;
        private string _parentContactEntityName;

        private int _parentContactCountryId;
        private string _parentContactCountryName;

        private int _qualificationLevelId;
        private string _qualificationLevelName;

        private int _qualificationFieldId;
        private string _qualificationFieldName;

        private int _qualificationSubFieldId;
        private string _qualificationSubFieldName;

        private int _fieldId;
        private string _fieldName;

        private bool _isRE;
        private bool _isEE;

        // to be handled later
        //private int _parentContactId; 

        #endregion

        #region Contact Details

        private int _languageCode;
        private string _contactName;
        private string _phoneNumber;
        private string _mobileNumber;
        private string _faxNumber;
        private string _email;
        private string _website;
        private string _description;
        private int _experience;
        private int _numberOfExperts;
        private string _address;
        private string _jobDescription;

        #endregion

        #endregion

        #region Public properties

        #region Contact Master

        public int ContactId
        {
            get { return _contactId; }
            set { _contactId = value; }
        }

        public int CountryId
        {
            get { return _countryId; }
            set { _countryId = value; }
        }
        public string CountryName
        {
            get { return _countryName; }
            set { _countryName = value; }
        }

        public int EntityId
        {
            get { return _entityId; }
            set { _entityId = value; }
        }
        public string EntityName
        {
            get { return _entityName; }
            set { _entityName = value; }
        }

        public int ParentContactId
        {
            get { return _parentContactId; }
            set { _parentContactId = value; }
        }
        public string ParentContactName
        {
            get { return _parentContactName; }
            set { _parentContactName = value; }
        }

        public int ParentContactEntityId
        {
            get { return _parentContactEntityId; }
            set { _parentContactEntityId = value; }
        }
        public string ParentContactEntityName
        {
            get { return _parentContactName; }
            set { _parentContactName = value; }
        }

        public int ParentContactCountryId
        {
            get { return _parentContactCountryId; }
            set { _parentContactCountryId = value; }
        }
        public string ParentContactCountryName
        {
            get { return _parentContactCountryName; }
            set { _parentContactCountryName = value; }
        }


        public int QualificationLevelId
        {
            get { return _qualificationLevelId; }
            set { _qualificationLevelId = value; }
        }
        public string QualificationLevelName
        {
            get { return _qualificationLevelName; }
            set { _qualificationLevelName = value; }
        }

        public int QualificationFieldId
        {
            get { return _qualificationFieldId; }
            set { _qualificationFieldId = value; }
        }
        public string QualificationFieldName
        {
            get { return _qualificationFieldName; }
            set { _qualificationFieldName = value; }
        }

        public int QualificationSubFieldId
        {
            get { return _qualificationSubFieldId; }
            set { _qualificationSubFieldId = value; }
        }
        public string QualificationSubFieldName
        {
            get { return _qualificationSubFieldName; }
            set { _qualificationSubFieldName = value; }
        }

        public int FieldId
        {
            get { return _fieldId; }
            set { _fieldId = value; }
        }
        public string FieldName
        {
            get { return _fieldName; }
            set { _fieldName = value; }
        }


        public bool IsRE
        {
            get { return _isRE; }
            set { _isRE = value; }
        }
        public bool IsEE
        {
            get { return _isEE; }
            set { _isEE = value; }
        }

        //To be handled later

        //public int ParentItemID
        //{
        //    get { return _parentContactId; }
        //    set { _parentContactId = value; }
        //}

        #endregion

        #region Contact Details

        public int LanguageCode
        {
            get { return _languageCode; }
            set { _languageCode = value; }
        }

        public string ContactName
        {
            get { return _contactName; }
            set { _contactName = value; }
        }

        public string PhoneNumber
        {
            get { return _phoneNumber; }
            set { _phoneNumber = value; }
        }

        public string MobileNumber
        {
            get { return _mobileNumber; }
            set { _mobileNumber = value; }
        }

        public string FaxNumber
        {
            get { return _faxNumber; }
            set { _faxNumber = value; }
        }

        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        public string Website
        {
            get { return _website; }
            set { _website = value; }
        }

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public int Experience
        {
            get { return _experience; }
            set { _experience = value; }
        }

        public int NumberOfExperts
        {
            get { return _numberOfExperts; }
            set { _numberOfExperts = value; }
        }

        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }

        public string JobDescription
        {
            get { return _jobDescription; }
            set { _jobDescription = value; }
        }

        #endregion

        #endregion

        #region Constructors

        public Contact()
        {

        }

        public Contact(DataRow dr)
        {
            try
            {
                //TODO: Replace ZEORS AND EmptyStrings WITH NULL

                this.ContactId = dr["ContactId"] != DBNull.Value ? Convert.ToInt32(dr["ContactId"]) : 0;
                this.ContactName = dr["ContactName"] != DBNull.Value ? Convert.ToString(dr["ContactName"]) : "";

                this.EntityId = dr["EntityId"] != DBNull.Value ? Convert.ToInt32(dr["EntityId"]) : 0;
                this.EntityName = dr["EntityName"] != DBNull.Value ? Convert.ToString(dr["EntityName"]) : "";

                this.CountryId = dr["CountryId"] != DBNull.Value ? Convert.ToInt32(dr["CountryId"]) : 0;
                this.CountryName = dr["CountryName"] != DBNull.Value ? Convert.ToString(dr["CountryName"]) : "";

                this.IsRE = Convert.ToBoolean(dr["IsRE"]);
                this.IsEE = Convert.ToBoolean(dr["IsEE"]);

                this.ParentContactId = dr["ParentContactId"] != DBNull.Value ? Convert.ToInt32(dr["ParentContactId"]) : 0;
                this.ParentContactName = dr["ParentContactName"] != DBNull.Value ? Convert.ToString(dr["ParentContactName"]) : "";

                this.ParentContactEntityId = dr["ParentContactEntityId"] != DBNull.Value ? Convert.ToInt32(dr["ParentContactEntityId"]) : 0;
                this.ParentContactEntityName = dr["ParentContactEntityName"] != DBNull.Value ? Convert.ToString(dr["ParentContactEntityName"]) : "";

                this.ParentContactCountryId = dr["ParentContactCountryId"] != DBNull.Value ? Convert.ToInt32(dr["ParentContactCountryId"]) : 0;
                this.ParentContactCountryName = dr["ParentContactCountryName"] != DBNull.Value ? Convert.ToString(dr["ParentContactCountryName"]) : "";

                this.QualificationLevelId = dr["QualificationLevelId"] != DBNull.Value ? Convert.ToInt32(dr["QualificationLevelId"]) : 0;
                this.QualificationLevelName = dr["QualificationLevelName"] != DBNull.Value ? Convert.ToString(dr["QualificationLevelName"]) : "";

                this.QualificationFieldId = this.QualificationLevelId = dr["QualificationFieldId"] != DBNull.Value ? Convert.ToInt32(dr["QualificationFieldId"]) : 0;
                this.QualificationFieldName = dr["QualificationFieldName"] != DBNull.Value ? Convert.ToString(dr["QualificationFieldName"]) : "";

                this.QualificationSubFieldId = this.QualificationLevelId = dr["QualificationSubFieldId"] != DBNull.Value ? Convert.ToInt32(dr["QualificationSubFieldId"]) : 0;
                this.QualificationSubFieldName = dr["QualificationSubFieldName"] != DBNull.Value ? Convert.ToString(dr["QualificationSubFieldName"]) : "";

                this.FieldId = dr["FieldId"] != DBNull.Value ? Convert.ToInt32(dr["FieldId"]) : 0;
                this.FieldName = dr["FieldName"] != DBNull.Value ? Convert.ToString(dr["FieldName"]) : "";

                this.LanguageCode = dr["LanguageCode"] != DBNull.Value ? Convert.ToInt32(dr["LanguageCode"]) : 0;

                this.PhoneNumber = dr["PhoneNumber"] != DBNull.Value ? Convert.ToString(dr["PhoneNumber"]) : "";
                this.MobileNumber = dr["MobileNumber"] != DBNull.Value ? Convert.ToString(dr["MobileNumber"]) : "";
                this.FaxNumber = dr["FaxNumber"] != DBNull.Value ? Convert.ToString(dr["FaxNumber"]) : "";

                this.Email = dr["Email"] != DBNull.Value ? Convert.ToString(dr["Email"]) : "";
                this.Website = dr["Website"] != DBNull.Value ? Convert.ToString(dr["Website"]) : "";

                this.Description = dr["Description"] != DBNull.Value ? Convert.ToString(dr["Description"]) : "";

                this.Experience = dr["Experience"] != DBNull.Value ? Convert.ToInt32(dr["Experience"]) : 0;
                this.NumberOfExperts = dr["NumberOfExperts"] != DBNull.Value ? Convert.ToInt32(dr["NumberOfExperts"]) : 0;

                this.Address = dr["Address"] != DBNull.Value ? Convert.ToString(dr["Address"]) : "";
                this.JobDescription = dr["JobDescription"] != DBNull.Value ? Convert.ToString(dr["JobDescription"]) : "";

            }
            catch (Exception exc)
            {

            }
        }

        #endregion

        #region Private methods
        #endregion

        #region Public methods

        #region Select

        /// <summary>
        /// Gets All Master Data related to specific Entity Id and Language Code
        /// </summary>
        /// <param name="entityId">Entity Id</param>
        /// <param name="languageCode">Language Code ( En-US : 1033) </param>
        /// <returns>DataTable Contains All contract Master details</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public DataTable GetAllMasterContacts(int entityId, int languageCode, int countryId)
        {
            return DataManager.GetAllMasterContacts(entityId, languageCode, countryId);
        }

        /// <summary>
        /// Gets All Contacts for specific entity, country & LanguageCode for the use of Web Service
        /// </summary>
        /// <param name="entityId">Entity Id</param>
        /// <param name="languageCode">Language Code: 1025 = Ar-SA , 1033 = Eng-US , 1036 = FR-Fr</param>
        /// <param name="countryId">Country Id</param>
        /// <returns>List of all Contacts matching the given parameters</returns>
        public List<Contact> GetAllContactsForWS(int entityId, int languageCode, int countryId)
        {
            // Gets all Contacts from database
            DataTable dtAllContacts = DataManager.GetAllMasterContacts(entityId, languageCode, countryId);

            // check for the return data
            if (dtAllContacts != null && dtAllContacts.Rows.Count > 0)
            {
                // if data table contains conatct(s) data
                List<Contact> _LContacts = new List<Contact>();

                // Lopp through all datatable rows
                foreach (DataRow dr in dtAllContacts.Rows)
                {
                    // fill a temp contact class with data using Constructor with DataRow Parameter
                    Contact _tempContact = new Contact(dr);
                    
                    // Add the temp class to the return list
                    _LContacts.Add(_tempContact);
                }

                // return the List contains all available contacts
                return _LContacts;
            }
            else
                return null;
        }

        /// <summary>
        /// Gets All data related to a Contact Id and Language Code
        /// </summary>
        /// <param name="contactId">Contract Id</param>
        /// <param name="languageCode">Language Code (Eng-US : 1033)</param>
        /// <returns>Data Table contains all Contract data</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
        public DataTable GetOneContractById(int contactId, int languageCode)
        {
            return DataManager.GetOneContactById(contactId, languageCode);
        }

        /// <summary>
        /// Gets All data related to a Contact Id and Language Code
        /// </summary>
        /// <param name="contactId">Contract Id</param>
        /// <param name="languageCode">Language Code (Eng-US : 1033)</param>
        /// <returns>Data Table contains all Contract data</returns>
        public Contact GetOneContractByIdForWS(int contactId, int languageCode)
        {
            // get contact data from database
            DataTable _dtContact =  DataManager.GetOneContactById(contactId, languageCode);

            if (_dtContact != null && _dtContact.Rows.Count > 0)
            {
                // copy data from data table into contact object
                Contact _tempContact = new Contact(_dtContact.Rows[0]);

                // return contact object
                return _tempContact;
            }
            else
                return null;
        }

        #endregion

        #region Update

        /// <summary>
        /// Update Contact object
        /// </summary>
        /// <param name="_contact">object contact contains the new data to be update</param>
        /// <returns>True if update was successful; False otherwise</returns>
        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public bool UpdateContact(Contact _contact)
        {
            return DataManager.UpdateContact(_contact);
        }

        #endregion

        #region Insert

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public bool InsertContact(Contact _contact)
        {
            return DataManager.InsertContact(_contact);
        }

        #endregion

        #region Delete
        public bool DeleteContact(Contact _contact)
        {
            return DataManager.DeleteContact(_contact);
        }
        #endregion

        #endregion

        #region Overrides
        #endregion
    }
}
